﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Final
{
    public enum S_Type { VAR, LABEL };

    class Symbol_Table
    {
        List<Symbol> symbols;

        public Symbol_Table()
        {
            symbols = new List<Symbol>();
        }

        public void Entry( S_Type type, String name, int position )
        {
            if (type == S_Type.VAR)
            {
                symbols.Add(new V_Symbol(name, position));
            }
            else
            {
                symbols.Add(new L_Symbol(name, position));
            }
        }

        public Symbol FindName(String name)
        {
            foreach (Symbol symbol in symbols)
            {
                if (symbol.Name == name)
                {
                    return symbol;
                }
            }

            return null;
        }

        public void Print()
        {
            Console.WriteLine("\tSymbol Table");

            foreach (Symbol s in symbols)
            {
                s.Print();
            }
        }
    }

    abstract class Symbol
    {
        protected int position;
        protected String name;
        protected S_Type type;

        public S_Type Type
        {
            get { return type; }
        }

        public String Name
        {
            get { return name; }
        }

        public int Location
        {
            get { return position; }
        }

        abstract public void Print();
    }

    class V_Symbol : Symbol
    {
        public V_Symbol(String name, int position)
        {
            type = S_Type.VAR;

            this.name = name;
            this.position = position;
        }

        public override void Print()
        {
            Console.WriteLine(name + '\t' + position + " VAR\t0");
        }
    }

    class L_Symbol : Symbol
    {
        bool found;

        public L_Symbol(String name, int position)
        {
            type = S_Type.LABEL;

            this.name = name;
            this.position = position;
            this.found = false;
        }

        public bool Found
        {
            get { return this.found; }
            set { this.found = value; }
        }

        public override void Print()
        {
            Console.WriteLine(name + '\t' + position + " LABEL\t0");
        }
    }
}

